//
//  XD_RecommendCollView.swift
//  XueDao
//
//  Created by wenwen on 2021/3/6.
//

import UIKit
import RxSwift

private let collViewCellIdentifier = "XD_RecommendCollViewCellID"

class XD_RecommendCollView: UIView {
    
    private lazy var disposeBag = DisposeBag()
    
    lazy var changeModel = XS_recommendCurriculumIpageM()
//    lazy var industryId = 0
    var curriculumId = -11 // 课程id
    var certificateId = -11 // 证书id
    var recruitmentId = -11 // 岗位id
    
    lazy var headerView: XD_RecommendHeaderView = {
        let headerView = XD_RecommendHeaderView()
        headerView.backgroundColor = .white
        headerView.changedBtn.addTarget(self, action: #selector(loadData), for: .touchUpInside)
        return headerView
    }()
    
    lazy var collections: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal
        flowLayout.itemSize = CGSize(width: .scale * 264, height: .scale * 88)
        flowLayout.minimumLineSpacing = .scale * 16
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: .scale * 16, bottom: .scale * 16, right: .scale * 16)
        
        let collections = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collections.showsVerticalScrollIndicator = false
        collections.showsHorizontalScrollIndicator = false
        collections.bounces = false
        collections.backgroundColor = .white
        collections.dataSource = self
        collections.delegate = self
        
        collections.register(XD_RecommendCollViewCell.self, forCellWithReuseIdentifier: collViewCellIdentifier)
        return collections
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(headerView)
        headerView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(.scale * 55)
        }
        
        addSubview(collections)
        collections.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reLoadData() {
        loadData()
    }
    
    @objc func loadData() {
        
//        let randomPages = Int.random(in: 0...changeModel.pages)
//        XS_NetWork.requestJson("/curriculum/replaceCurriculumRecommend", parameters: ["pages": randomPages, "industryId":industryId])
        let tmpCurriculumId = curriculumId == -11 ? "" : "\(curriculumId)"
        let tmpCertificateId = certificateId == -11 ? "" : "\(certificateId)"
        let tmpRecruitmentId = recruitmentId == -11 ? "" : "\(recruitmentId)"
        XS_NetWork.requestJson("/curriculum/queryReplaceCurriculum", parameters: ["curriculumId" :  tmpCurriculumId, "certificateId" : tmpCertificateId, "recruitmentId" : tmpRecruitmentId])
            .mapModel(type: XS_queryByIdM.self)
            .subscribe(onNext: { [weak self] model, resp in
                guard let `self` = self, resp.success, let models = model else { return }
                self.changeModel = models.recommendCurriculumIpage

                self.collections.setContentOffset(.zero, animated: false)
                self.collections.reloadData()
            }).disposed(by: disposeBag)
    }
}

extension XD_RecommendCollView: UICollectionViewDataSource, UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.changeModel.records.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collViewCellIdentifier, for: indexPath) as! XD_RecommendCollViewCell

        let recommendCurriculumIpageRecordsM = changeModel.records[indexPath.item]
        cell.titleLab.text = recommendCurriculumIpageRecordsM.curriculumName
        cell.detailLab.text = recommendCurriculumIpageRecordsM.introduce
        cell.iconImgV.sd_setImage(with: URL(string: recommendCurriculumIpageRecordsM.curriculumImgUrl))
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let recommendCurriculumIpageRecordsM = changeModel.records[indexPath.item]
        let vc = recommendCurriculumIpageRecordsM.curriculumForm == "online" ? XS_KeChengVC() : XS_KCXianxiaVC()
        vc.id = "\(recommendCurriculumIpageRecordsM.id)"
        XS_NVC.shared.pushViewController(vc, animated: true)
    }
}
